package ae.pcfc.cldos.online.web;

import static java.lang.String.format;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import org.apache.commons.lang.ClassUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.ModelAndView;

import ae.pcfc.cldos.online.common.exception.SytemException;

/**
 * 
 * @author ammar
 *
 */
public class ViewUtils {

	/*
	 * so steps will be as follows 1- go define ViewPath in ViewPath (if not
	 * already defined) like company/register/nonclient 2- if the pageType is
	 * not defined go define it in PageType like add,edit,form,etc 3- use
	 * getForward or getRedirect 4- define a constant value for view if it does
	 * not follow a pattern (use with caution)
	 */

	// in case forward view we use folder name constant like
	// COMPANY_REGISTERATION_NON_CLIENT_<VIEW NAME LIKE LIST,ADD,..>_VIEW
	//public static final String COMPANY_REGISTERATION_NON_CLIENT_LIST_VIEW = format("%s/%s", ViewPath.COMPANY_REGISTRATION_NON_CLIENT,PageType.LIST);
	// in case redirect view in un patterned name we use folder name constant
	// like COMPANY_REGISTERED_<VIEW NAME LIKE LIST,ADD,..>_REDIRECT_VIEW
	//public static final String COMPANY_REGISTERED_SHOW_REDIRECT_VIEW = format("redirect:../%s/%s", ViewPath.COMPANY_REGISTERED, PageType.SHOW);
	
	public static final ModelAndView DEFAULT_VIEW = null;

	private ViewUtils() {
	}

	public static String getForward(String viewPath, PageType pageType) {
		String viewName = ((viewPath.substring(1)).replace('/', '.'))
				.toLowerCase();
		return format("%s.%s", viewName, pageType.getPageType()
				.toLowerCase());
	}

	public static String getForward(String viewPath, PageType pageType,
			Map<String, String> paramMap) {
		StringBuilder parms = new StringBuilder();
		for (String key : paramMap.keySet()) {
			parms.append(key + "=" + paramMap.get(key));

		}
		String viewName = ((viewPath.substring(1)).replace('/', '.'))
				.toLowerCase();
		return format("%s.%s.htm?%s", viewName, pageType.getPageType()
				.toLowerCase(), parms.toString());
	}

	public static String getRedirect(String viewPath, PageType pageType) {
		return format(ViewPath.__REDIRECT + "%s/%s.htm", viewPath, pageType.getPageType()
				.toLowerCase());
	}

	public static String getRedirect(String viewPath, PageType pageType,
			Map<String, String> paramMap) {
		StringBuilder parms = new StringBuilder();
		for (String key : paramMap.keySet()) {
			try {
				
				if(parms.length() > 0){
					parms.append("&");
				}
				parms.append(key + "="
						+ URLEncoder.encode(paramMap.get(key), "UTF-8"));

			} catch (UnsupportedEncodingException e) {
				parms.append(key + "=" + paramMap.get(key));
			}

		}

		return format(ViewPath.__REDIRECT + "%s/%s.htm?%s", viewPath, pageType.getPageType()
				.toLowerCase(), parms.toString());

	}

	/*
	 * public static String decodeParam(String param) {
	 * 
	 * }
	 */

	public static String addExceptionError(SytemException exception,
			BindingResult errors) {
		
		String message = null;

		String messageKey = "";// Constants.EXCEPTION_MESSAGES_PREFIX;
		String exceptionKey = ClassUtils.getShortClassName(exception, null)
				.toLowerCase();
		if (exceptionKey == null) {
			return message;
		}

		messageKey += exceptionKey;
		if (exception != null && exception.getErrorMessageArgList().size()!=0) {

			errors.reject(messageKey,
					exception.getErrorMessageArgList().toArray(new String[0]), null);
		} 

		return message;
	}

}
